package com.cheyun.netsalev3.viewmodel.aftersale

import android.content.Intent
import android.os.Looper
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.aftersale.MaintenanceDetail
import com.cheyun.netsalev3.bean.basedata.Member
import com.cheyun.netsalev3.repository.aftersale.MaintenanceDetailsRepository
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.ToastUtil
import com.cheyun.netsalev3.view.aftersale.EditMaintenanceActivity
import com.cheyun.netsalev3.viewmodel.BaseViewModel
import com.cheyun.netsalev3.widget.MyContentDialog
import com.cheyun.netsalev3.widget.MyDialog
import com.jeremyliao.liveeventbus.LiveEventBus
import java.io.Serializable

/**
 * @author Michael
 * @date  2018/12/19 - 9:41.
 */
class MaintenanceDetailsActivityViewModel(var maintenanceDetailsRepository: MaintenanceDetailsRepository) : BaseViewModel() {

    var detail = ObservableField<MaintenanceDetail>()
    var maintenanceData:MaintenanceDetail? = null

    var phoneMsg = MutableLiveData<String>()
    var state = MutableLiveData<Int>()

    var memberlist = ArrayList<Member>()

    init {
        initData()

        MySharedPreferences.getBaseData()?.member?.apply {
            memberlist.addAll(this)
        }
    }

    /**
     * 初始化数据
     */
    fun initData() {
        maintenanceDetailsRepository.getData(this::setData)
    }

    private fun setData(item: MaintenanceDetail) {
        detail.set(item)
        maintenanceData = item

        phoneMsg.postValue(item.phone)
        state.postValue(item.state)
    }

    private var currentView:View? = null
    fun onClickPhone(view: View) {
//        currentView = view
        detail.get()?.apply {
            var phone = this.phone
            if(phone.isNullOrEmpty()){
                ToastUtil.toastShortMessage("号码无法拨打")
            }else{
                FunctionUtils.callPhone(view.context!!, phone)
            }
        }
    }

    fun onClickEdit(view: View){
        var intent = Intent(view.context, EditMaintenanceActivity::class.java)
        maintenanceData.apply {
            intent.putExtra("data", maintenanceData as Serializable)
        }
        view.context.startActivity(intent)
    }

    fun onClickConfirm(view: View){
        var dialog = MyContentDialog.newInstance("提示","是否确认预约")
        dialog.onClickOk = this@MaintenanceDetailsActivityViewModel::confirmOrder
        dialog.show((view.context as AppCompatActivity).supportFragmentManager, "")
    }

    fun confirmOrder(){
        var moth: () -> Unit = {
            initData()
            LiveEventBus.get().with("aftersale_maintenance_refresh").postValue("")
        }
        var fail: () -> Unit = {

        }
        maintenanceDetailsRepository.confirm(maintenanceData?.id!!,moth,fail)
    }

    fun onClickCancel(view: View){
        currentView = view
        maintenanceDetailsRepository.cancelInfo(maintenanceData?.vin!!,this@MaintenanceDetailsActivityViewModel::cancelInfoBack)
    }
    fun cancelInfoBack(total:Int){
        var title = "是否确认取消预约"
        if(total >0){
            title = "当前客户存在上门取车/上门送车订单，是否确认取消保养预约"
        }
        var dialog = MyContentDialog.newInstance("提示",title)
        dialog.onClickOk = this@MaintenanceDetailsActivityViewModel::cancelOrder
        dialog.show((currentView?.context as AppCompatActivity).supportFragmentManager, "")
    }

    fun cancelOrder(){
        var moth: () -> Unit = {
            initData()
            LiveEventBus.get().with("aftersale_maintenance_refresh").postValue("")
        }
        var fail: () -> Unit = {

        }
        maintenanceDetailsRepository.cancel(maintenanceData?.id!!,moth,fail)
    }

    fun onClickShare(view: View){
        var content = view.context
        if (content is AppCompatActivity) {
            var list = ArrayList<DialogParam>()
            for (item in memberlist) {
                if(item.isadmin == 0) {
                    var dialogParam = DialogParam(item.realname, item.uid.toString())
                    list.add(dialogParam)
                }
            }
            var myDialog = MyDialog.newInstance("请选择分配人员", list)
            myDialog.huidia = this::rcvName
            myDialog.show(content.supportFragmentManager, "")
        }
    }

    /**
     * 接待人回调
     */
    fun rcvName(item: DialogParam?) {
        var moth: () -> Unit = {
            initData()
            LiveEventBus.get().with("aftersale_maintenance_refresh").postValue("")
        }
        var fail: () -> Unit = {

        }
        item?.apply {
            maintenanceDetailsRepository.allocation(maintenanceData?.id.toString(),item.id,moth,fail)
        }
    }
}